document.ready(function() {
    webSpinner.spin(getItem('loadSpin'));
    listSpinner.spin(getItem('loading'));

    if (getStorage('electronic_id') != null && getStorage('electronic_id') != '') {
        //详情页返回加载
    	if (parseInt(getStorage('electronic_page')) > 10) {
    		setStorage('electronic_page', '10');
    	}
    	resetList();
    }
    else {
    	//首次进入加载
    	queryKey('ok');
    }
});

function resetList() {
    resetBottomStatus('loading');

    var param = {};
    param.team = 'platform';
    param.ctrl = 'electronic';
    param.func = 'resetElectronicList';
    param.area = encodeURIComponent(getStorage('electronic_areaInfo'));
    param.category = encodeURIComponent(getStorage('electronic_categoryInfo'));
    param.feature = encodeURIComponent(getStorage('electronic_featureInfo'));
    param.keyword = encodeURIComponent(getStorage('electronic_searchKeyword'));
    param.page = encodeURIComponent(getStorage('electronic_page'));

    sendAjax(param, function(result) {
        var list = result.electronicList;
        electronic.list = list;

        setHtml('listCount', result.listCount);
        resetBottomStatus('reset', 19, list);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });
}

function queryKey(flag) {
    resetPageBar(flag);
    resetBottomStatus('loading');

    var param = {};
    param.team = 'platform';
    param.ctrl = 'electronic';
    param.func = 'getElectronicList';
    param.area = getAjaxValue('areaInfo');
    param.category = getAjaxValue('categoryInfo');
    param.feature = getAjaxValue('featureInfo');
    param.keyword = getAjaxValue('searchKeyword');
    param.page = getAjaxValue('page');

    sendAjax(param, function(result) {
        var list = result.electronicList;
        if (getValue('page') == '1' || electronic.list == '') {
            electronic.list = list;
            location.href = "#mainBg";
            setValue('page', '1');
        }
        else {
            if (list.length > 0) {
                for (var i = 0; i < list.length; i++) {
                    electronic.list.push(list[i]);
                }
            }
        }

        setHtml('listCount', result.listCount);
        resetBottomStatus('bottomed', 19, list, electronic.list);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });

    if (flag == 'search') {
        submitSearchKeyword(getValue('searchKeyword'), 'electronic');
    }
}

function slipsearch_clearOption() {
    //重置筛选值
    setValue('areaInfo', '');
    setValue('categoryInfo', '');
    setValue('featureInfo', '');

    //重置筛选种类样式
    var titleList = ['区域', '类别', '特征'];
    for (var i = 0; i < 3; i++) {
        setClass('optionItemText' + (i + 1), 'option-item-text');
        getItem('optionItemimg' + (i + 1)).src = 'static/pic/wap/option_down.png';
        setHtml('optionItemText' + (i + 1), titleList[i]);
    }

    //重置筛选项样式-区域
    var objList = getItem('optionItemFloatDiv1').children;
    for (var i = 0; i < objList.length; i++) {
        if (i == 0) {
            objList[i].getElementsByTagName('div')[0].className = 'select-text-click';
            objList[i].getElementsByTagName('img')[0].style.display = '';
        }
        else {
            objList[i].getElementsByTagName('div')[0].className = 'select-text';
            objList[i].getElementsByTagName('img')[0].style.display = 'none';
        }
    }
    setDisplay('optionItemFloatDiv1', 'none');

    //重置筛选项样式-类别
    var parentObj = getItem('optionItemFloatDiv2');
    objList = parentObj.children[0].children[0].children;
    for (var i = 0; i < objList.length; i++) {
        objList[i].className = 'option-select';
    }
    setValue('categoryInfo', '');
    setOptionTitle('categoryInfo');
    setDisplay('optionItemFloatDiv2', 'none');

    //重置筛选项样式-特征
    parentObj = getItem('optionItemFloatDiv3');
    objList = parentObj.children[0].children[0].children;
    for (var i = 0; i < objList.length; i++) {
        objList[i].className = 'option-select';
    }
    setValue('featureInfo', '');
    setOptionTitle('featureInfo');
    setDisplay('optionItemFloatDiv3', 'none');

    setDisplay('maskOptionDiv', 'none');
    optionIndex = '0';
}

function viewElectronic(id) {
    setStorage('electronic_page', getValue('page'));
    setStorage('electronic_id', id);
    setStorage('electronic_areaInfo', getValue('areaInfo'));
    setStorage('electronic_categoryInfo', getValue('categoryInfo'));
    setStorage('electronic_featureInfo', getValue('featureInfo'));
    setStorage('electronic_searchKeyword', getValue('searchKeyword'));

    toUrl('index.php?m=electronic&c=info&from=list&electronicid=' + id);
}

function selectMoreOption(obj, text, id) {
    if (obj.className == 'option-select') {
        obj.className = 'option-selected';
        setOptionValue(text, id, 'add');
    }
    else {
        obj.className = 'option-select';
        setOptionValue(text, id, 'del');
    }

    setOptionTitle(id);
}

function setOptionTitle(valueid) {
    var titleid = '', title = '';
    if (valueid == 'categoryInfo') {
        titleid = 'optionItemText2';
    }
    else if (valueid == 'featureInfo') {
        titleid = 'optionItemText3';
    }

    var valueInfo = getValue(valueid);
    if (valueInfo == '') {
        if (valueid == 'categoryInfo') {
            title = '类别';
        }
        else if (valueid == 'featureInfo') {
            title = '特征';
        }
    }
    else {
        valueInfo = valueInfo.replaceAll('●●', '@');
        valueInfo = valueInfo.replaceAll('●', '');
        var count = valueInfo.split('@').length;
        title = '已选(' + count + ')';
    }

    setHtml(titleid, title);
}

function setOptionValue(text, id, flag) {
    if (flag == 'add') {
        setValue(id, getValue(id) + '●' + text + '●');
    }
    else {
        setValue(id, getValue(id).replace('●' + text + '●', ''));
    }
}

function saveCategory() {
    closeOption();
    queryKey('ok');
}

function resetCategory(obj) {
    var parentObj = obj.parentNode.parentNode;
    var objList = parentObj.children[0].children[0].children;

    for (var i = 0; i < objList.length; i++) {
        objList[i].className = 'option-select';
    }

    setValue('categoryInfo', '');
    setOptionTitle('categoryInfo');

    closeOption();
    queryKey('ok');
}

function saveFeature() {
    closeOption();
    queryKey('ok');
}

function resetFeature(obj) {
    var parentObj = obj.parentNode.parentNode;
    var objList = parentObj.children[0].children[0].children;

    for (var i = 0; i < objList.length; i++) {
        objList[i].className = 'option-select';
    }

    setValue('featureInfo', '');
    setOptionTitle('featureInfo');
    
    closeOption();
    queryKey('ok');
}

function resetOption() {
    var value = '', title = '';

    //重置区域选项
    value = getValue('areaInfo');
    if (value == '') {
        title = '区域';
    }
    else {
        title = value;

        var objList = getItem('optionItemFloatDiv1').children;
        objList[0].getElementsByTagName('div')[0].className = 'select-text';
        objList[0].getElementsByTagName('img')[0].style.display = 'none';
        for (var i = 0; i < objList.length; i++) {
            if (value == objList[i].getElementsByTagName('div')[0].innerHTML) {
                objList[i].getElementsByTagName('div')[0].className = 'select-text-click';
                objList[i].getElementsByTagName('img')[0].style.display = '';
                break;
            }
        }
    }
    setHtml('optionItemText1', title);

    //重置类别选项
    value = getValue('categoryInfo');
    if (value == '') {
        title = '类别';
    }
    else {
        value = value.replaceAll('●●', '@');
        value = value.replaceAll('●', '');
        title = '已选(' + value.split('@').length + ')';

        var objList = getItem('optionItemFloatDiv2').children[0].children[0].children;
        for (var i = 0; i < objList.length; i++) {
            if (value.includes(objList[i].innerHTML)) {
                objList[i].className = 'option-selected';
            }
        }
    }
    setHtml('optionItemText2', title);
    
    //重置特征选项
    value = getValue('featureInfo');
    if (value == '') {
        title = '特征';
    }
    else {
        value = value.replaceAll('●●', '@');
        value = value.replaceAll('●', '');
        title = '已选(' + value.split('@').length + ')';

        var objList = getItem('optionItemFloatDiv3').children[0].children[0].children;
        for (var i = 0; i < objList.length; i++) {
            if (value.includes(objList[i].innerHTML)) {
                objList[i].className = 'option-selected';
            }
        }
    }
    setHtml('optionItemText3', title);

    //当搜索框有值时，显示搜索框
    value = getValue('searchKeyword');
    if (value != '') {
        setDisplay('slipSearchBar', '');
    }
}